package com.showcai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.ApplyWatchMapper;
import com.showcai.def.Constant;
import com.showcai.module.Apply;
import com.showcai.module.ApplyWatch;
import com.showcai.service.ApplyService;
import com.showcai.service.OrderService;
import com.showcai.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-9-12.
 */
@Service("applyWatchService")
public class ApplyWatchServiceImpl extends ApplyServiceImpl implements ApplyService{
    @Autowired
    ApplyWatchMapper applyWatchMapper;
    @Autowired
    UserService userService;
    @Override
    public Apply apply(Apply apply) {
        ApplyWatch applyWatch=(ApplyWatch)apply;
        applyWatch.setCreateTime(new Date());
        super.insertBaseApply(applyWatch);
        applyWatch.setApplyId(apply.getTid());
        applyWatch.setTid(null);
        applyWatch.setStatus(OrderService.UNPAY);
        applyWatchMapper.insertApplyWatch(applyWatch);
        return applyWatch;
    }

    @Override
    public Apply selectApply(Map<String, String> map) {
        return applyWatchMapper.selectApply(map);
    }

    @Override
    public Apply selectApplyByOrderId(Long orderId) {
        return applyWatchMapper.selectApplyByOrderId(orderId);
    }

    @Override
    public List<ApplyWatch> selectApplyList(Map<String, String> map) {
        List list = applyWatchMapper.selectApplyWatchList(map);
        return list;
    }

    @Override
    public void modifyApply(Apply apply) {
        ApplyWatch applyWatch=(ApplyWatch)apply;
        applyWatchMapper.updateApplyWatchByPrimaryKeySelective(applyWatch);
    }

    @Override
    public PageInfo selectApplyPageList(Map<String,String> map) {
        String pageStr = map.get("page");
        String pageSizeStr = map.get("pageSize");
        Integer page = null;
        Integer pageSize = null;
        if (StringUtils.isNotBlank(pageStr) && StringUtils.isNumeric(pageStr)) {
            page = Integer.parseInt(pageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr) && StringUtils.isNumeric(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        if (null == page) {
            page = Constant.PAGENUM;
        }
        if (null == pageSize) {
            pageSize = Constant.PAGESIZENum;
        }
        PageHelper.startPage(page, pageSize);
        List<ApplyWatch> list = applyWatchMapper.selectApplyWatchList(map);
        PageInfo<ApplyWatch> pageinfo = new PageInfo<>(list);
        return pageinfo;
    }

}
